package org.gpp.proj1.robot.action.executor;

import java.util.List;

import org.gpp.proj1.robot.Robot;
import org.gpp.proj1.robot.action.PartAction;
import org.gpp.proj1.robot.action.step.ActionStep;
import org.gpp.proj1.robot.part.EquippedPart;

/**
 * Thread that executes an action set
 *  
 * @author Paolo Victor, paolovictor@gmail.com
 */
public class ActionExecutor implements Runnable {

	/**
	 * The executor's target
	 */
	private EquippedPart parentRobot;

	/**
	 * The action that will be executed
	 */
	private PartAction action;

	/**
	 * The enemy targets. If null or empty there will be no targets
	 * considered
	 */
	private List<Robot> targets;

	private boolean finished;

	/**
	 * Default consctrutor
	 * 
	 * @param action The action that will be executed
	 * @param target The target robot
	 */
	public ActionExecutor( EquippedPart target, PartAction action ) {
		this.action = action;
		this.parentRobot = target;
	}
	
	/**
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		this.finished = false;
		
		List<ActionStep> steps = action.getSteps();
		
		if( steps != null ) {
			for( ActionStep step : steps ) {
				step.execute( parentRobot, targets );
			}
		}
		
		this.finished = true;
	}

	public void setTargets(List<Robot> targets) {
		this.targets = targets;
	}

	public boolean isFinished() {
		
		return this.finished;
	}
}